package heima3_2.cn.osc.ui.main_fragment.DynamicFragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.oschina.app.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import heima3_2.cn.osc.base.BaseFragment;
import heima3_2.cn.osc.constant.ResultState;
import heima3_2.cn.osc.model.bean.DynamicFragmentBean.Tweet;
import heima3_2.cn.osc.presenter.DynamicPresenter;
import heima3_2.cn.osc.ui.adapter.DynamicAdapter;
import heima3_2.cn.osc.ui.view.AutoLoadListView;
import heima3_2.cn.osc.utils.Global;

/**
 * Created by 超能浣熊 on 2017/1/13.
 */
//TODO:
public class DynamicFragment extends BaseFragment implements AutoLoadListView.Pagingable{

    public AutoLoadListView listViewsDyn;
    @Bind(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    //TODO:
    int index = 0;  //加载网络的次数，0代表初次加载

    private int uid=0;
    private int type;
    public static int fragmentPage=1;
//        private RecyclerViewAdapter adapter;
    public DynamicAdapter dynamicAdapter;
    private DynamicPresenter dynamicPresenter;
    public static String TABLAYOUT_FRAGMENT = "tab_fragment1";


    Handler h = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    swipeLayout.setRefreshing(false);
                    dynamicAdapter.notifyDataSetChanged();
                    break;
                default:
                    break;
            }

        }
    };
    private List<Tweet> mTweetList;


    public static DynamicFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(TABLAYOUT_FRAGMENT, page);
        fragmentPage=page;
        DynamicFragment fragment = new DynamicFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt(TABLAYOUT_FRAGMENT);
    }

   //dsds

    @Override
    protected ResultState onLoadSubData() {
        return ResultState.LOAD_SUCCESS;
    }

    @Override
    protected View onCreateSubSuccessView(LayoutInflater inflater, ViewGroup container) {
        View listView = Global.inflats(R.layout.fragment_swiperefreshlayout);
        ButterKnife.bind(this, listView);
        listViewsDyn = (AutoLoadListView) listView.findViewById(R.id.listViews_dyn);
        listViewsDyn.setPagingableListener(this);

        switch (type){
            case 1:
                uid=0;
                break;
            case 2:
                uid=1;
                break;
            case 3:
                uid=2;
                break;
        }
        dynamicAdapter = new DynamicAdapter(context, null);
        dynamicPresenter = new DynamicPresenter(this,uid);

        //TODO:
        dynamicPresenter.getData(index);    //这是初始加载的数据

        initView(listView);

        mTweetList = dynamicPresenter.mTweetList;


        listViewsDyn.setHasMoreItems(true);
        return listView;
    }


    private void initView(View listView) {
        switch (type) {
            case 1:
                listViewsDyn.setAdapter(dynamicAdapter);
                swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                h.sendEmptyMessage(1);
                            }
                        }).start();
                    }
                });
                break;
            case 2:
                listViewsDyn.setAdapter(dynamicAdapter);
                break;
            case 3:
                listViewsDyn.setAdapter(dynamicAdapter);
                break;
            default:
                break;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onLoadMoreItems() {
        dynamicPresenter.getData(index++);
        Log.d("aaaaa","aaaaaaaaaaaaa  "+index);

    }
}
